package com.springsource.boot.run;

import lombok.Data;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.properties.bind.Bindable;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.io.support.ResourcePropertySource;

import java.io.IOException;

public class Step6 {

    public static void main(String[] args) throws IOException {

        SpringApplication app = new SpringApplication();
        StandardEnvironment env = new StandardEnvironment();
        env.getPropertySources().addLast(new ResourcePropertySource("classpath:steps/step4.properties"));

        // 演示将配置文件中的键值对绑定到实体类中
        // 自动创建对象并绑定
        Binder binder = Binder.get(env);
        User user = binder.bind("user", User.class).get();
        System.out.println(user);
        // 绑定既有对象
        User user1 = new User();
        binder.bind("user", Bindable.ofInstance(user1));
        System.out.println(user1);
    }

    @Data
    static class User {
        private String firstName;
        private String middleName;
        private String lastName;
    }


}
